package com.ftg.learn.day15.interfacexml.question;

import com.ftg.learn.day15.interfacexml.question.domain.DeptDomain;
import com.ftg.learn.day15.interfacexml.question.domain.EmpDomain;
import com.ftg.learn.day15.interfacexml.question.mapper.DeptMapper;
import com.ftg.learn.day15.interfacexml.question.mapper.EmpMapper;
import org.apache.ibatis.binding.MapperRegistry;
import org.apache.ibatis.datasource.pooled.PooledDataSource;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.*;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;

import javax.sql.DataSource;
import java.util.List;

/**
 * @author 布衣
 */
public class TestMain {

    public static void main(String[] args) {
        Configuration configuration = new Configuration();

        configuration.setLogImpl(org.apache.ibatis.logging.stdout.StdOutImpl.class);

        configuration.addMapper(DeptMapper.class);
        configuration.addMapper(EmpMapper.class);

        PooledDataSource datasources = new PooledDataSource();
        datasources.setDriver("com.mysql.jdbc.Driver");
        datasources.setUsername("root");
        datasources.setPassword("123456");
        datasources.setUrl("jdbc:mysql://localhost:3306/db2402");

        Environment e = new Environment("development", new JdbcTransactionFactory(), datasources);

        configuration.setEnvironment(e);

        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory factory =builder.build(configuration);
        try(
                SqlSession sqlSession = factory.openSession();
        ){
//            EmpMapper emp = sqlSession.getMapper(EmpMapper.class);
//            System.out.println(emp.findManyByDeptId(10));
            DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);
            List<DeptDomain> deptDomains = mapper.showAll();

            for (int i = 0; i < deptDomains.size(); i++) {
                System.out.print(deptDomains.get(i).getDeptId()+"部门编号\t");
                System.out.print(deptDomains.get(i).getDname()+"部门名称\t");
                System.out.println(deptDomains.get(i).getEmps().size()+"人数\t");

                System.out.println("人员信息");
                List<EmpDomain> emps = deptDomains.get(i).getEmps();
                for (int j = 0; j < emps.size(); j++) {
                    System.out.print(emps.get(j).getEname()+"员工姓名\t");
                }
                System.out.println();
            }

        }catch (Exception e1){
            e1.printStackTrace();
        }
    }

}

